/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Deplacement;
import Model.Equipe;
import Model.Saison;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "ControllerDeplacement", urlPatterns = {"/Deplacement/*"})
public class ControllerDeplacement extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();

        if (pathInfo.equals("/add"))
        {
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/leader/dep-add.jsp");
            rd.forward(request, response);
        }
        else if (pathInfo.equals("/addForm")){
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/leader/dep-add.jsp");
            // Récupération des données du formulaire
            String destination = request.getParameter("destination");
            String kms = request.getParameter("kms");
            
            // Vérification des données et sauvegarde en BDD
            if (destination != null && kms != null){
                if(!destination.trim().equals("") && !kms.trim().equals("")){
                    // A FAIRE
                    
                    request.setAttribute("message", "<div id=\"success\"><b>Opération réussie.</b><br />Le déplacement a été ajouté avec succès.</div>");
                }
                else
                {
                    request.setAttribute("message", "<div id=\"failure\"><b>Une erreur est survenue.</b><br />Un ou plusieurs champs sont manquants !</div>");
                }
            }
            else
            {
                request.setAttribute("message", "<div id=\"failure\"><b>Une erreur est survenue.</b><br />Des éléments sont NULL !</div>");
            }
            // redirect
            rd.forward(request, response);
                     
        }
        /*
        else if (pathInfo.equals("/delete")){
            String id = request.getParameter("id");
            if(id != null){
                
            }
        }
        */
        else
        {
            List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
            List<Deplacement> ld = new ArrayList<Deplacement>();
            
            for(Saison s : ls){
                ld.addAll(s.getDeplacementList());
            }
            request.setAttribute("listDeplacements", ld);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/gamer/dep-list.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
